<template>
  <div class="index" v-loading.fullscreen.lock="bodyloading">
    <div class="main">
      <div>
        <p class="tit">门店名称：</p>
        <p>{{ materialsApply.custName }}</p>
      </div>
      <div>
        <p class="tit">门店编码：</p>
        <p>{{ materialsApply.custCode }}</p>
      </div>
      <div>
        <p class="tit">小微：</p>
        <p>{{ materialsApply.tradeName }}</p>
      </div>
      <div>
        <p class="tit">渠道：</p>
        <p>{{ materialsApply.channelName }}</p>
      </div>
      <div>
        <p class="tit">产业：</p>
        <p>{{ materialsApply.industryName }}</p>
      </div>
      <div>
        <p class="tit">活动主题：</p>
        <p>
          {{ materialsApply.themeName
          }}<s v-if="themeIsValid == 'N'">(已过期)</s>
        </p>
      </div>
    </div>
    <div class="splx" v-if="state == '2099'">
      <p class="title">物流信息</p>
      <div class="text">
        <div>
          <p class="tit">收件人:</p>
          <p>{{ materialsApply.receiveUserName }}</p>
        </div>
        <div>
          <p class="tit">收件人电话:</p>
          <p>{{ materialsApply.receiveUserPhone }}</p>
        </div>
        <div>
          <p class="tit">收货地址:</p>
          <p>
            {{
              materialsApply.receiveProvinceName +
              materialsApply.receiveCityName +
              materialsApply.receiveCountyName +
              materialsApply.receiveDetailsAddress
            }}
          </p>
        </div>
      </div>
    </div>
    <div class="splx">
      <p class="title">审批立项</p>
      <van-steps
        direction="vertical"
        :active="0"
        active-color="#7ac0f3"
        :class="{ current: currentstate }"
      >
        <van-step v-for="(item, index) in operationLogVO.list" :key="index">
          <h3>{{ item.operationResult }}</h3>
          <p v-if="item.operationOpinion">
            审批意见 {{ item.operationOpinion }}
          </p>
          <p>操作人 {{ item.operationUserName }}({{ item.operationUserId }})</p>
          <p>{{ item.operationTime }}</p>
        </van-step>
      </van-steps>

      <div class="more" v-show="operationLogVOstate" @click="more">
        {{ currentstate ? "收起↑" : "更多↓" }}
      </div>
    </div>

    <div class="prdlist">
      <div v-for="item in materials" :key="item.modelCode">
        <div class="top">
          <div class="img">
            <img :src="item.picAddr" alt="" />
          </div>
          <div class="text">
            <p class="tit">{{ item.modelName }}</p>
            <p class="code">{{ item.modelCode }}</p>
            <p class="desc">物料描述：{{ item.modelDis }}</p>
          </div>
        </div>
        <div class="bottom">
          <p>申请数量</p>
          <p>x{{ item.number }}</p>
        </div>
        <div class="bottom" v-if="item.lastNumber">
          <p>审批数量</p>
          <p>x{{ item.lastNumber }}</p>
        </div>
      </div>
    </div>
    <!-- <div class="main">
      <p class="title">预算体确认</p>
      <div>
        <p class="tit">小微:</p>
        <p>{{ queryform.mdname }}</p>
      </div>
      <div>
        <p class="tit">产业:</p>
        <p>{{ queryform.mdname }}</p>
      </div>
      <div>
        <p class="tit">费用类型：</p>
        <p>{{ queryform.mdname }}</p>
      </div>
      <div>
        <p class="tit">预算体:</p>
        <p>{{ queryform.mdcode }}{{ queryform.mdname }}</p>
      </div>
    </div> -->
    <div class="splx qssc" v-if="state == '2016'">
      <p class="title">签收资料上传</p>
      <div class="text" v-if="!isEdit">
        <div>
          <p style="width: 100%; font-size: 0.12rem">
            见证性资料，如签收时的包裹照片，问题照片(最多上传16张)
          </p>
        </div>
      </div>
      <div class="uppic">
        <div
          v-show="piclist.length >= 1"
          v-for="(item, index) in piclist"
          :key="item.index"
        >
          <img :src="item.picAddr" @click="scale(index)" />
          <van-icon
            name="clear"
            class="del"
            v-show="item.picAddr"
            @click="delItem(item, item.index)"
            v-if="!isEdit"
          />
        </div>
        <div v-if="!isEdit">
          <img
            v-show="piclist.length <= 16"
            fit="contain"
            src="../../assets/images/add-dashed.png"
            @click="openImgSelect"
          />
        </div>
      </div>
      <div class="textarea">
        <van-field
          v-model="message"
          rows="3"
          autosize
          type="textarea"
          :disabled="isEdit"
          placeholder="备注描述"
          maxlength="50"
          show-word-limit
        />
      </div>
      <p class="title">评价</p>
      <div class="evaluatelist">
        <div v-for="item in evaluate" :key="item.index">
          <p class="tit">{{ item.typeName }}</p>
          <van-rate
            v-model="item.num"
            :size="25"
            color="#ffd21e"
            void-icon="star"
            void-color="#eee"
            :readonly="isEdit"
          />
        </div>
      </div>
    </div>
    <div class="main" v-if="state == '2099'">
      <div>
        <p class="tit">签收人：</p>
        <p>
          {{ materialsApply.signUserName }}({{ materialsApply.signUserCode }})
        </p>
      </div>
      <div>
        <p class="tit">提交时间：</p>
        <p>{{ materialsApply.signTime }}</p>
      </div>
    </div>
    <div class="btnbox" v-if="state == 2003">
      <van-button type="info" @click="Modify">修改</van-button>
    </div>
    <div class="btnbox">
      <van-button type="info" v-if="state == 2016" @click="Signfor"
        >确认签收</van-button
      >
    </div>
    <van-action-sheet
      v-model="show"
      :actions="actions"
      cancel-text="取消"
      title="选择上传方式"
      :closeable="false"
      close-on-click-action
      @select="imgSelect"
    ></van-action-sheet>
  </div>
</template>

<script>
import { materialsApplyDetails, qianShouMaterials } from "../../api/app";
import { readyApi } from "hwork-widget-report-component/src/utils/index";
import { ImagePreview } from "vant";
import { set } from "js-cookie";
export default {
  name: "Index",
  components: {},

  data() {
    return {
      header: {
        show: true,
        return: true,
        search: false,
        title: "营销物料申请详情",
        home: "/index",
        close: true,
      },
      currentstate: false,
      bodyloading: false,
      logisticsInfo: "", //物流信息
      materials: "", //物料信息
      materialsApply: "", //物料申请主信息
      operationLogVO: "", //审核立项信息
      themeIsValid: "", //主题是否有效
      isHome: false,
      show: false,
      state: this.$route.query.state,
      applyId: this.$route.query.id,
      actions: [
        { name: "从相册选择", number: 0 },
        { name: "拍摄照片", number: 1 },
      ],
      piclist: [],
      operationLogVOstate: false,
      message: "",
      evaluate: [
        {
          applyId: this.$route.query.id,
          typeCode: "01",
          num: 0,
          typeName: "工艺、材质、VI标准程度",
        },
        {
          applyId: this.$route.query.id,
          typeCode: "02",
          num: 0,
          typeName: "数量、尺寸、规格标准程度",
        },
      ],
      isEdit: false,
      qsstate: false,
    };
  },
  created() {
    this.getdetail();
    if (this.state == "2099") {
      this.isEdit = true;
    } else if (this.state == "2016") {
      this.qsstate = true;
    } else {
    }
  },
  mounted() {},

  methods: {
    more() {
      this.currentstate = !this.currentstate;
    },
    getdetail() {
      this.bodyloading = true;
      let json = {
        applyId: this.applyId,
      };
      materialsApplyDetails(json).then((res) => {
        // console.log(res);
        if ((res.code = 200)) {
          this.logisticsInfo = res.data.logisticsInfo;
          this.materials = res.data.materials;
          this.operationLogVO = res.data.operationLogVO;
          this.todoUsers = this.operationLogVO.todoUsers;
          let todoUsers = []
          if (this.todoUsers) {
            this.todoUsers.forEach((item) => {
              let json = {
                operationResult: `${this.operationLogVO.todo}审批`,
                operationUserName: item.userName,
                operationUserId: item.userCode,
              };
              todoUsers.push(json)
            });
          }
          this.operationLogVO.list = [...todoUsers,...this.operationLogVO.list]
          if (this.operationLogVO.list.length > 3) {
            this.operationLogVOstate = true;
          }
          this.materialsApply = res.data.materialsApply;
          this.themeIsValid = res.data.themeIsValid; //Y 有效 N 无效 提示重新申请
          this.bodyloading = false;
          if (this.state == "2099") {
            this.piclist = res.data.pictureList;
            this.evaluate = res.data.scoreList;
            this.message = res.data.materialsApply.remark;
          }
        }
      });
    },
    // 打开下拉照片方式
    openImgSelect() {
      if (this.piclist.length < 16) {
        this.show = true;
      } else {
        this.$toast("最多上传16张图片");
      }
    },
    // 选择照片方式
    imgSelect(val) {
      let number = 16;
      if (this.piclist.length > 0) {
        number -= this.piclist.length;
      }
      const that = this;
      // 判断
      let path = "hwork_dev/zxgl/";
      if (process.env.NODE_ENV === "production") {
        path = "hsimg/zxgl/";
      }
      if (val.number === 0) {
        // 从相册选择
        that.operationType = "album";
      } else if (val.number === 1) {
        // 拍照上传
        that.operationType = "camera";
      }
      const req = {
        operationType: that.operationType,
        count: number,
        savePath: path,
      };
      readyApi(() => {
        window.AlipayJSBridge.call("openCameraOrAlbum", req, (res) => {
          if (res.code === 200) {
            res.data.forEach((item) => {
              that.piclist.push({
                picAddr: item,
                picType: "QS",
                applyId: this.applyId,
              });
            });
            this.$forceUpdate();
          }
        });
      });
    },
    delItem(item, index) {
      // console.log(item, index);
      this.piclist.splice(index, 1);
      this.$forceUpdate();
    },
    scale(index) {
      let arr = [];
      this.piclist.forEach((item) => {
        arr.push(item.picAddr);
      });
      ImagePreview({
        images: [...arr],
        startPosition: index,
      });
    },
    Modify() {
      if (this.themeIsValid == "Y") {
        let dto1 = {
          selectFlag: "1",
          code: this.materialsApply.custCode,
          name: this.materialsApply.custName,
        };
        let dto2 = {
          selectFlag: "2",
          code: this.materialsApply.industryCode,
          name: this.materialsApply.industryName,
        };
        let dto3 = {
          selectFlag: "3",
          code: this.materialsApply.themeCode,
          name: this.materialsApply.themeName,
        };
        this.$store.dispatch("selectOption/setSelectOptions", dto1);
        this.$store.dispatch("selectOption/setSelectCustomer", dto2);
        this.$store.dispatch("selectOption/setSelectBrand", dto3);
        this.$router.push({
          name: "submission",
          query: {
            applyId: this.applyId,
            state: "002",
          },
        });
      } else {
        this.$toast("当前主题已过期，请重新申请");
      }
    },
    Signfor() {
      let that = this;
      if (this.piclist.length == 0) {
        this.$toast("请上传图片");
        return false;
      }
      // console.log(this.evaluate);
      for (var i = 0; i < this.evaluate.length; i++) {
        if (this.evaluate[i].num == 0) {
          this.$toast("请对" + this.evaluate[i].typeName + "评级");
          return false;
        }
      }
      let json = {
        picList: this.piclist,
        remark: this.message,
        scoreList: this.evaluate,
      };
      // console.log(json);
      // return false
      this.bodyloading = true;
      qianShouMaterials(json).then((res) => {
        // console.log(res);
        if (res.code == 200) {
          that.$toast(res.msg);
          setTimeout(function () {
            that.$router.push({
              name: "Index",
              query: {
                state: "004",
              },
            });
          }, 1000);
        } else {
          that.$toast(res.msg);
        }
        this.bodyloading = false;
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.index {
  font-size: 0.14rem;
  > div {
    width: 95vw;
    margin: 0 auto;
    border-radius: 0.03rem;
    background-color: #fff;
    margin-bottom: 0.1rem;
    padding-bottom: 0.05rem;
  }

  .main {
    overflow: hidden;
    .title {
      margin: 0;
      font-size: 0.14rem;
      display: block;
      width: 100%;
      margin-top: 0.1rem;
      font-weight: 700;
      padding: 0 0.15rem;
      padding-bottom: 0.1rem;
      box-sizing: border-box;
      border-bottom: 1px solid #eee;
    }
    > div {
      display: flex;
      justify-content: flex-start;
      align-items: center;
      position: relative;
      height: 0.3rem;
      padding: 0 0.15rem;
      box-sizing: border-box;
      .tit {
        width: 5em;
        text-align: left;
      }
      s {
        text-decoration: none;
        color: red;
      }
      .icon {
        position: absolute;
        font-size: 0.14rem;
        position: absolute;
        right: 0.05rem;
      }
    }
  }
  .splx {
    overflow: hidden;
    padding-bottom: 0;
    .more {
      width: 100%;
      height: 0.4rem;
      line-height: 0.4rem;
      text-align: center;
      border-top: 1px solid #eee;
    }
    p {
      margin: 0;
      font-size: 0.14rem;
    }
    .van-steps {
      max-height: 2.2rem;
    }
    .current {
      max-height: none;
    }
    .title {
      margin: 0;
      font-size: 0.14rem;
      display: block;
      width: 100%;
      margin-top: 0.1rem;
      font-weight: 700;
      padding: 0 0.15rem;
      padding-bottom: 0.1rem;
      box-sizing: border-box;
      border-bottom: 1px solid #eee;
    }
    .text {
      padding: 0.05rem 0;
      > div {
        display: flex;
        justify-content: flex-start;
        position: relative;
        padding: 0 0.15rem;
        box-sizing: border-box;
        p {
          width: 2.5rem;
          line-height: 0.3rem;
        }
        .tit {
          width: 1rem;
          text-align: left;
        }
      }
    }
    .textarea {
      width: 100%;
      padding: 0 0.15rem;
      .van-field {
        border: 1px solid #eee;
        box-sizing: border-box;
        padding: 0.05rem;
      }
    }
    .uppic {
      width: 100%;
      display: flex;
      justify-content: flex-start;
      align-items: center;
      flex-wrap: wrap;
      padding: 0 0.15rem;
      box-sizing: border-box;
      margin-top: 0.1rem;
      > div {
        position: relative;
        width: 0.5rem;
        margin-right: 0.1rem;
        margin-bottom: 0.1rem;
        .del {
          font-size: 0.14rem;
          position: absolute;
          right: 0;
          top: 0;
        }
        img {
          width: 0.5rem;
          height: 0.5rem;
          display: block;
        }
      }
    }
    h3 {
      font-size: 0.14rem;
    }
    .evaluatelist {
      width: 100%;
      padding-bottom: 0.1rem;
      > div {
        padding: 0 0.15rem;
        box-sizing: border-box;
        margin-top: 0.1rem;
        > p {
          margin-bottom: 0.05rem;
          font-size: 0.12rem;
        }
      }
    }
  }
  .qssc {
    .title {
      margin-bottom: 0.1rem;
    }
    .text {
      padding: 0;
      > div {
        p {
          line-height: normal;
        }
      }
    }
  }
  .prdlist {
    background: none;
    margin-bottom: 0.05rem;
    > div {
      padding: 0.1rem;
      box-sizing: border-box;
      border-radius: 0.03rem;
      background-color: #fff;
      margin-bottom: 0.1rem;
      &:last-child {
        margin-bottom: 0;
      }
      .top {
        display: flex;
        justify-content: space-between;
        align-items: center;
        >>> .van-checkbox {
          width: 0.2rem;
          height: 0.2rem;
        }
        .img {
          width: 0.7rem;
          height: 0.7rem;
          img {
            width: 100%;
            height: 100%;
            display: flex;
          }
        }
        .text {
          width: 2.3rem;
          p {
            margin: 0;
          }
          .tit {
            color: #f40;
          }
          .code {
            color: #666;
            font-size: 0.12rem;
            margin: 0.05rem 0;
          }
          .desc {
            color: #333;
          }
        }
      }
      .bottom {
        display: flex;
        justify-content: space-between;
        align-items: center;
        p {
          margin: 0;
        }
      }
    }
  }
  .btnbox {
    background: none;
    margin-top: 0.4rem;
    .van-button {
      width: 100%;
      display: block;
      height: 0.4rem;
      border-radius: 0.4rem;
      color: #fff;
      text-align: center;
      line-height: 0.4rem;
      margin-top: 0.05rem;
      background-color: #2698eb;
    }
  }
  .van-popup {
    width: 100vw;
    margin-bottom: 0;
    padding-bottom: 0;
  }
}
</style>
